﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Xml;

namespace MassIntellect.WorkIt
{
    public class Pipe : IPipe
    {
        private class NullPipe : Pipe
        {            
            public override string Name
            {
                get
                {
                    return "";
                }
                set
                {

                }
            }

            public override Scope Scope
            {
                get
                {
                    return Scope.Private;
                }
                set
                {

                }
            }
			public override event Action<PulseArgs> OnPulse
            {
                add { }
                remove { }
            }
            public override void Pulse(PulseArgs args)
            {

            }
        }
        public const string DEFAULT_PIPENAME = "";
        public readonly static Pipe Null = new NullPipe();
        public readonly static IEnumerable<object> NULL_PARAMS = null;
        public readonly static IEnumerable<KeyValuePair<string, object>> EMPTY_PARAMS = new ReadOnlyCollection<KeyValuePair<string, object>>(new KeyValuePair<string, object>[] { });

        #region IPipe Members

        [Browsable(true)]
        [Description("Наименование элемента.")]
        //[DisplayName("Имя")]
        [Serialize(SerializeAs.Attribute)]
        public virtual string Name { get; set; }

        [Browsable(true)]
        [Description("Область видимости")]
        //[DisplayName("Видимость")]
        [DefaultValue(typeof(Scope), "Private")]
        [Serialize(SerializeAs.Attribute)]
        public virtual Scope Scope { get; set; }

        [Browsable(true)]
        [Description("Активность элемента. Данное свойство, означает, что элемент может сам стать инициатором отсыла сигнала без предварительного получения другого сигнала.")]
        //[DisplayName("Активность")]
        [DefaultValue(false)]
        [Serialize(SerializeAs.Attribute)]
        public virtual bool IsActive { get; set; }

        public void Pulse(IPipe sender, IEnumerable<PulsePrm> prms)
        {
            Pulse(new PulseArgs(sender).AddParams(prms));
        }

        public virtual void Pulse(PulseArgs args)
        {
            if (_OnPulse != null)
                _OnPulse(args);
        }

		protected Action<PulseArgs> _OnPulse;

        /// <summary>
        /// Слушающие методы
        /// </summary>
        [Browsable(false)]
        public virtual IEnumerable<Delegate> PulseListeners
        {
            get
            {
                if (_OnPulse != null)
                    return _OnPulse.GetInvocationList();
                else
                    return new Delegate[0];
            }
        }

		public virtual event Action<PulseArgs> OnPulse
        {
            add
            {
                _OnPulse += value;
            }
            remove
            {
                _OnPulse -= value;
            }
        }

        public virtual void Activate()
        {
            Pulse(new PulseArgs(this));
        }

        public virtual T GetInterface<T>()
            where T:class
        {
            return this as T;
        }        

        #endregion
    }
}
